/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dto.PackBoxStackData;
import com.sigmatrix.h5.enums.PackScanOperTypeEnum;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.DevanningPackingBoxService;
import com.sigmatrix.h5.service.DismantlePileStackService;
import com.sigmatrix.h5.service.PackManageService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.vo.PackBoxStackVo;

/**
 *@ClassName: PackManageServiceImpl
 *@Description: TODO()
 *@author youjun
 *@date 2017年8月18日 上午1:18:49
 */
@Service(value="packManageService")
public class PackManageServiceImpl extends BaseServiceImpl<PackBoxStackData> implements PackManageService {

	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(PackManageServiceImpl.class);
	
	/**拆补箱业务层接口*/
	@Autowired
	private transient DevanningPackingBoxService devanningPackingBoxService;
	
	/**拆补垛业务层接口*/
	@Autowired
	private transient DismantlePileStackService dismantlePileStackService;
	
	/**(non-Javadoc)
	 *@Title: upload
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.PackManageService#upload(com.sigmatrix.sc.handheld.inter.web.vo.packmanage.PackBoxStackVo)
	 *@author youjun
	 *@date 2017年8月18日 上午1:20:33
	 */
	@Override
	public Response upload(final PackBoxStackVo packBoxStackVo) throws ServiceException {

		final Response validateResponse = validateUploadParams(packBoxStackVo);
		if(validateResponse.getCode()!=Cons.RESPONSE_CODE_SUCCESS) {
			return validateResponse;
		}
		try{
			final int operationType = packBoxStackVo.getOperationType(); // 操作类型:组箱、拆箱、组垛、补垛等
			if(operationType==PackScanOperTypeEnum.GROUP_OR_FILL_BOX.getId()
					||operationType==PackScanOperTypeEnum.SPLIT_BOX.getId()) {
				return devanningPackingBoxService.upload(packBoxStackVo); // 拆补箱操作
			}
			return dismantlePileStackService.upload(packBoxStackVo); // 拆补垛操作
		}catch(Exception e){
			LOGGER.error("upload fail", e);
			throw new ServiceException(e);
		}
	}

	/**
	 *@Title: validateUploadParams
	 *@Description: TODO()
	 *@return Response
	 *@author youjun
	 *@date 2017年8月18日 上午1:21:53
	 *@param packBoxStackVo
	 *@return
	 */
	private Response validateUploadParams(final PackBoxStackVo packBoxStackVo) {

		final Response validateResponse = super.validateBasicParams(packBoxStackVo, true, true);
		if(validateResponse.getCode()!=Cons.RESPONSE_CODE_SUCCESS) {
			return validateResponse;
		}
		if(StringUtils.isBlank(packBoxStackVo.getCode())) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "操作对象码不能为空");
		}
		if(StringUtils.isBlank(packBoxStackVo.getOperateCodes())) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "上传的操作码不能为空");
		}
		if(StringUtils.isBlank(packBoxStackVo.getWorkOrder())) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "工单号不能为空");
		}
		if(packBoxStackVo.getProductId()==null){
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "操作的产品ID不能为空");
		}
		if(packBoxStackVo.getWarehouseId()==null) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "操作的仓库ID不能为空");
		}
		return new Response(ResponseEnum.SUCCESS);
	}

	
}
